//关闭计时器 
function closeTimer(jishiqi) {
	window.clearInterval(jishiqi);
}

//等待司机接单时候的动画展示 
function waitDriver() {
	$("body")
			.append(
					"<div id='waitDriver' class=\"inputAddressBox wait\" style=\"background:#000;opacity:0.1;position: fixed;top:0;left:0;\"></div>");
	$("body")
			.append(
					"<div class=\"inputAddressBox wait\" style=\"position: fixed;top:0;left:0;\"><div class='waitBox'><div class='waitBox-header'>等待司机接单</div><div class='waitBox-content'><p class='vmiddle'><span class='fa-stack fa-lg'><i style='-webkit-animation:fa-spin 5s infinite linear;animation:fa-spin 5s infinite linear;' class='fa fa-ban text-danger fa-li fa fa-spinner fa-spin'></i></span> 等待接单中...</p></div><div class='waitBox-foot'><button id='cancelTrip' style='margin-top:10px;' class='weui_btn weui_btn_warn weui_btn_mini'>取消订单</button></div></div></div>");
}

/* 取消行程 */
function cancelTrip(tripId) {
	$.ajax({
		url : "/Wchat/functionalBusinessController/cancelTrip.do?tripId="
				+ tripId,
		success : function(json) {
			$(".wait").remove();// 去除时间等待动画
			closeTimer(jishiqi);// 顺便关闭计时器
		}
	});
}

//轮询服务端,获取接单的司机
function getTrip(json) {
	tripId = json.id;// 乘客的行程ID
	jishiqi = setInterval(
			function() {
				$
						.ajax({
							url : "/Wchat/functionalBusinessController/getTrip.do?tripId="
									+ json.id,
							success : function(json) {
									if (json.status == 1) {
										
										$(".waitBox>.waitBox-content .vmiddle")
												.html(
														"<a style='color:green;font-size:12px;'>有司机接单,即将跳转到支付界面</a>");
										window.location.href = "userCenterCarpooling.html?openid="+getParam(location.href,"openid");
									}
							}
						});
			}, 2000);
}

/*
 * 发布拼车信息 需要如下参数 ： 
 * @param startAddress 		开始位置 
 * @param startDetailAddress开始位置详情
 * @param startLatitude 	开始位置坐标 纬度
 * @param startLongitude 	开始位置坐标 经度 
 * @param endAddress 		结束位置 
 * @param endDetailAddress  开始位置详情 
 * @param endLatitude 		结束位置坐标 纬度
 * @param endLongitude  	结束位置坐标 经度 
 * @param miles 			里程(公里数) 
 * @param estimateCost 		预计费用
 * @param planStartDatetime 预计开始时间 
 * @param planEndDatetime 	预计到达时间 
 * @param passengerNums 	乘客人数
 */
function submitCarpoolingInfo() {
	console.log(startAddress);
	console.log(startDetailAddress);
	console.log(endAddress);
	console.log(endDetailAddress);
	console.log("开始纬度:" + start_lat + " ;开始经度:" + start_lng);
	console.log("结束纬度:" + end_lat + " ;结束经度:" + end_lng);
	console.log("预计费用:" + estimateCost);
	console.log("里程:" + miles);
	
	planStartDatetime=$("#planStartDatetime").val();
	
	var  ddate = new Date(planStartDatetime).getTime();//将时间转换成时间戳long值
	
	planStartDatetime = ddate;
	
	if (start_lat == null || start_lat == "") {
		 $.toptip('必须选择起始位置', 'warning');
		return;
	}

	if (start_lng == null || start_lng == "") {
		 
		return;
	}

	if (end_lat == null || end_lat == "") {
		$.toptip('必须选择结束位置', 'warning');
		return;
	}

	if (end_lng == null || end_lng == "") {
		return;
	}
	
	var url = "/Wchat/functionalBusinessController/addTrip.do?startAddress="
			+ startAddress + "&startDetailAddress=" + startDetailAddress
			+ "&startLatitude=" + start_lat + "&startLongitude=" + start_lng
			+ "&endDetailAddress=" + endDetailAddress + "&endAddress="
			+ endAddress + "&endLatitude=" + end_lat + "&endLongitude="
			+ end_lng + "&passengerNums=1&miles=" + miles + "&estimateCost="
			+ estimateCost+"&planStartDatetime="+planStartDatetime + "&driverEstimateIncome=" + driverEstimateIncome;
	
	console.log(url);
	
	$.ajax({
		url : url,
		type:'POST',
		contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
		success : function(json) {
			if(json.success==true){
				getTrip(json);
				localStorage.tripId_p=json.id;//将行程ID存入客户端
				waitDriver();
			}else{
				$.toptip(json.msg, 'warning');
				switch(json.type){
				case "NOLOGIN":
					$.toptip("登录超时,即将为您转到登录界面", 'warning');
					location.href = "userLogin.html";
					break;
				case "NETERROR":
					$.toptip(json.msg, 'warning');
					break;
				}
			}
			
		}
	});
}

/*地址录入*/
function addLuru() {
	document.getElementsByClassName("inputAddressBox")[0].style.display = "block";
}

/* 关闭弹出窗口 */
function canel(clas) {
	document.getElementsByClassName(clas)[0].style.display = "none";
}

/* 登录 */
function login() {
	$.ajax({
		url : "/Wchat/functionalBusinessController/login.do?token="+localStorage.token,
		success : function(json) {
			//将登录成功之后的token存入客户端缓存
			json=$.parseJSON(json);
			//localStorage.v8sharetoken=json.token;
		}
	});
}

/* 初始化地址列表 */
function initAddressList(keyword) {
	$.ajax({
		url : "/Wchat/AuxiliaryController/addressExchange.do?keyword="
				+ keyword,
		success : function(json) {

			if (json.status == false) {
				return;
			}

			var list = json.content.data;
			$("#srarchAddressResult").empty();
			if (list.length < 1) {
				return;
			}
			var str = "";
			for (var i = 0; i < list.length; i++) {
				str = str + "<li class='address' startAddress='"
						+ list[i].title + "' startDetailAddress='"
						+ list[i].address + "' endlat='" + list[i].location.lat
						+ "' endlng='" + list[i].location.lng
						+ "'><span class=\"addressMain\">" + list[i].title
						+ "</span><span class=\"addressFu\">" + list[i].address
						+ "</span></li>";
			}
			$.hideLoading();//加载完成.
			$("#srarchAddressResult").append(str);
		}
	});
}

//关闭地图匹配界面
function closeMap() {
	$("#container").hide();
}

/* 计算费用并显示值到固定元素 */
function calculatingCost() {

	if (start_lat == null || start_lat == "") {
		return;
	}

	if (start_lng == null || start_lng == "") {
		return;
	}

	if (end_lat == null || end_lat == "") {
		return;
	}

	if (end_lng == null || end_lng == "") {
		return;
	}

	$.ajax({
		url : "/Wchat/functionalBusinessController/calculatingCost.do",
		data : {
			start_lat : start_lat,
			start_lng : start_lng,
			end_lat : end_lat,
			end_lng : end_lng
		},
		success : function(json) {
			$("#estimatedCost").text("￥" + json.estimateCost);
			var date = new Date();//当前时间
			date.setMinutes(date.getMinutes()+Math.ceil(json.miles));//累加时间
			var timeStr = formatDateTime(date);//转换格式
			$("#miles").text(timeStr);
			estimateCost = json.estimateCost;// 将预计费用赋值给成员变量
			miles = json.miles;// 将里程赋值给成员变量
			driverEstimateIncome = json.driverEstimateIncome;//给司机预计收入赋值
			$.hideLoading();//加载完成.
		}
	});
}

/*查询本地缓存中是否有openid存在，
* 如果没有存在,则跳转到微信认证界面
* 认证,然后得到openid再存入本地
*/
function checkOpenid(){
	var openid=getParam(location.href,"openid");
	if(openid==null || openid.length<20){
		//window.location.href="errorPrompt.html";
	}

	var location_openid=localStorage.openid;
	
	/*if(location_openid!=null && location_openid!=""){
		//去服务器检查是否有手机号绑定，如果没有绑定就跳转到手机号绑定界面.
		$.ajax({
			url:"/Wchat/functionalBusinessController/checkLogin.do?openid="+location_openid,
			success:function(json){
				if(json.success==true){//此 用户已经绑定手机号,现在帮他登录
					var userinfo=json.data;
					localStorage.userInfo=userinfo;
				}else{//没有绑定手机号,跳转到手机号绑定界面
					window.location.href="boundPhone.html";
				}
			}
		});
		
		
		
		
		return;
	}
	
	if(openid==null || openid==''){
		window.location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx0ffed81e00322daa&redirect_uri=http://flysli.eicp.net/Wchat/authentication/getUserInfo.do&response_type=code&scope=snsapi_userinfo&state=321#wechat_redirect";
	}else{
		localStorage.openid=openid;
		window.location.href="boundPhone.html";
	}*/
	
}